<template>
  <div class="development-config-container">
    <!-- 优化的页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <div class="header-icon">
            <experiment-outlined />
          </div>
          <div class="header-text">
            <h1>开发设置</h1>
            <p>配置开发环境相关的功能，包括开发菜单的显示控制和调试工具等</p>
          </div>
        </div>
        <div class="header-right">
          <div class="status-indicator" :class="{ active: showDevMenus }">
            <div class="status-dot"></div>
            <span>{{ showDevMenus ? '开发模式已启用' : '开发模式已禁用' }}</span>
          </div>
        </div>
      </div>
    </div>

    <div class="config-content">
      <div class="config-grid">
        <!-- 主控制面板 -->
        <div class="control-panel">
          <div class="panel-header">
            <div class="panel-title">
              <span class="panel-icon">🎛️</span>
              <span>开发菜单总控制</span>
            </div>
            <a-switch 
              v-model:checked="showDevMenus" 
              @change="handleDevMenuToggle"
              :loading="saving"
              size="default"
              class="main-switch"
            />
          </div>
          
          <div class="panel-content">
            <div class="control-description">
              <p>控制左侧导航栏中所有开发和测试相关菜单的显示状态</p>
              <div class="feature-tags">
                <a-tag color="blue">开发测试</a-tag>
                <a-tag color="green">故事驱动</a-tag>
                <a-tag color="purple">妙码测试</a-tag>
              </div>
            </div>
            
            <div class="quick-actions" v-if="showDevMenus">
              <a-button 
                type="text" 
                size="small" 
                @click="toggleAllMenus"
                class="quick-action-btn"
              >
                <template #icon>
                  <rocket-outlined v-if="!allMenusEnabled" />
                  <api-outlined v-else />
                </template>
                {{ allMenusEnabled ? '全部关闭' : '全部开启' }}
              </a-button>
            </div>
          </div>
        </div>

        <!-- 菜单项配置区域 -->
        <div class="menu-items-section" v-if="showDevMenus">
          <div class="section-header">
            <h3>菜单项详细配置</h3>
            <div class="section-subtitle">个性化定制每个开发菜单的显示状态</div>
          </div>

          <div class="menu-items-grid">
            <!-- 开发测试菜单 -->
            <div class="menu-item-card" :class="{ active: menuToggles.devTest }">
              <div class="card-header">
                <div class="menu-icon dev-test-icon">
                  <tool-outlined />
                </div>
                <div class="menu-switch">
                  <a-switch 
                    v-model:checked="menuToggles.devTest" 
                    @change="(checked) => handleMenuToggle('devTest', checked)"
                    size="small"
                  />
                </div>
              </div>
              <div class="card-content">
                <h4 class="menu-name">开发测试</h4>
                <p class="menu-description">静态聚合码牌测试、动态聚合码牌测试、支付测试工具等</p>
                <div class="menu-stats">
                  <a-tag size="small" color="blue">15+ 个工具</a-tag>
                  <a-tag size="small" color="cyan">测试环境</a-tag>
                </div>
              </div>
            </div>

            <!-- 故事驱动菜单 -->
            <div class="menu-item-card" :class="{ active: menuToggles.storyDriven }">
              <div class="card-header">
                <div class="menu-icon story-driven-icon">
                  <rocket-outlined />
                </div>
                <div class="menu-switch">
                  <a-switch 
                    v-model:checked="menuToggles.storyDriven" 
                    @change="(checked) => handleMenuToggle('storyDriven', checked)"
                    size="small"
                  />
                </div>
              </div>
              <div class="card-content">
                <h4 class="menu-name">故事驱动</h4>
                <p class="menu-description">支付产品管理、渠道管理、商户管理等故事驱动开发功能</p>
                <div class="menu-stats">
                  <a-tag size="small" color="green">8 个模块</a-tag>
                  <a-tag size="small" color="lime">管理工具</a-tag>
                </div>
              </div>
            </div>

            <!-- 妙码测试菜单 -->
            <div class="menu-item-card" :class="{ active: menuToggles.miaomaTest }">
              <div class="card-header">
                <div class="menu-icon miaoma-test-icon">
                  <api-outlined />
                </div>
                <div class="menu-switch">
                  <a-switch 
                    v-model:checked="menuToggles.miaomaTest" 
                    @change="(checked) => handleMenuToggle('miaomaTest', checked)"
                    size="small"
                  />
                </div>
              </div>
              <div class="card-content">
                <h4 class="menu-name">妙码测试</h4>
                <p class="menu-description">妙码自动生成模块的测试和调试功能，代码生成器等</p>
                <div class="menu-stats">
                  <a-tag size="small" color="purple">动态模块</a-tag>
                  <a-tag size="small" color="magenta">代码生成</a-tag>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 开发工具配置区域 -->
      <div class="dev-tools-section">
        <div class="section-header">
          <h3>
            <tool-outlined class="section-icon" />
            开发工具配置
          </h3>
          <div class="section-subtitle">调整开发和调试相关的工具设置</div>
        </div>

        <div class="tools-grid">
          <!-- 调试工具卡片 -->
          <div class="tool-category-card">
            <div class="category-header">
              <div class="category-icon debug-icon">
                <monitor-outlined />
              </div>
              <h4>调试工具</h4>
            </div>
            <div class="category-content">
              <div class="tool-item">
                <div class="tool-info">
                  <div class="tool-name">调试日志</div>
                  <div class="tool-desc">控制台显示详细调试信息</div>
                </div>
                <a-switch 
                  v-model:checked="debugSettings.enableDebugLog" 
                  size="small" 
                  :checkedChildren="'ON'" 
                  :unCheckedChildren="'OFF'"
                />
              </div>
              <div class="tool-item">
                <div class="tool-info">
                  <div class="tool-name">性能监控</div>
                  <div class="tool-desc">监控页面性能和API响应时间</div>
                </div>
                <a-switch 
                  v-model:checked="debugSettings.enablePerformanceMonitor" 
                  size="small"
                  :checkedChildren="'ON'" 
                  :unCheckedChildren="'OFF'"
                />
              </div>
            </div>
          </div>

          <!-- API工具卡片 -->
          <div class="tool-category-card">
            <div class="category-header">
              <div class="category-icon api-icon">
                <cloud-outlined />
              </div>
              <h4>API 工具</h4>
            </div>
            <div class="category-content">
              <div class="tool-item">
                <div class="tool-info">
                  <div class="tool-name">Mock数据模式</div>
                  <div class="tool-desc">使用模拟数据进行开发测试</div>
                </div>
                <a-switch 
                  v-model:checked="debugSettings.useMockData" 
                  size="small"
                  :checkedChildren="'ON'" 
                  :unCheckedChildren="'OFF'"
                />
              </div>
              <div class="tool-item">
                <div class="tool-info">
                  <div class="tool-name">请求日志</div>
                  <div class="tool-desc">记录所有API请求和响应</div>
                </div>
                <a-switch 
                  v-model:checked="debugSettings.logAPIRequests" 
                  size="small"
                  :checkedChildren="'ON'" 
                  :unCheckedChildren="'OFF'"
                />
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 底部操作区域 -->
      <div class="action-section">
        <div class="action-header">
          <h3>配置操作</h3>
          <div class="action-subtitle">保存或重置您的开发设置配置</div>
        </div>
        
        <div class="action-buttons">
          <div class="primary-actions">
            <a-button 
              type="primary" 
              @click="handleSave" 
              :loading="saving" 
              size="large"
              class="save-btn"
            >
              <template #icon>
                <save-outlined />
              </template>
              保存所有设置
            </a-button>
            
            <a-button 
              @click="handleReset" 
              size="large"
              class="reset-btn"
            >
              <template #icon>
                <redo-outlined />
              </template>
              重置工具设置
            </a-button>
          </div>
          
          <div class="danger-actions">
            <a-popconfirm
              title="确定要清空所有开发设置吗？"
              description="这将重置所有开发菜单和工具配置，此操作不可撤销。"
              @confirm="handleResetDevSettings"
              ok-text="确定"
              cancel-text="取消"
            >
              <a-button 
                danger 
                :loading="saving"
                size="large"
                class="danger-btn"
              >
                <template #icon>
                  <delete-outlined />
                </template>
                清空所有设置
              </a-button>
            </a-popconfirm>
          </div>
        </div>

        <div class="action-tips">
          <a-alert
            message="提示"
            description="配置更改会立即生效，无需手动保存。但建议定期保存以防止意外数据丢失。"
            type="info"
            show-icon
            closable
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, computed } from 'vue'
import { message } from 'ant-design-vue'
import { 
  ExperimentOutlined, 
  ToolOutlined, 
  RocketOutlined, 
  ApiOutlined,
  RedoOutlined,
  SaveOutlined,
  DeleteOutlined,
  MonitorOutlined,
  CloudOutlined
} from '@ant-design/icons-vue'
import { useDevMenus } from '@/composables/useDevMenus'

const { showDevMenus, menuToggles, toggleDevMenus, toggleMenuItem, resetSettings } = useDevMenus()

const saving = ref(false)

// 开发工具配置
const debugSettings = reactive({
  enableDebugLog: false,
  enablePerformanceMonitor: false,
  useMockData: true,
  logAPIRequests: false
})

// 检查是否所有菜单都启用
const allMenusEnabled = computed(() => {
  return Object.values(menuToggles).every(value => value)
})

const handleSave = async () => {
  saving.value = true
  try {
    // 保存开发工具配置到localStorage
    localStorage.setItem('dev-tools-settings', JSON.stringify(debugSettings))
    
    await new Promise(resolve => setTimeout(resolve, 800)) // 模拟保存
    message.success('开发设置已保存')
  } catch (error) {
    message.error('保存失败，请重试')
  } finally {
    saving.value = false
  }
}

const handleReset = () => {
  // 重置开发工具配置
  Object.assign(debugSettings, {
    enableDebugLog: false,
    enablePerformanceMonitor: false,
    useMockData: true,
    logAPIRequests: false
  })
  message.info('已重置为默认设置')
}

const handleDevMenuToggle = (checked: boolean) => {
  toggleDevMenus(checked)
  message.success(checked ? '开发菜单已启用' : '开发菜单已禁用')
}

const handleMenuToggle = (menuKey: keyof typeof menuToggles, checked: boolean) => {
  console.log('Menu toggle:', menuKey, checked) // 调试日志
  toggleMenuItem(menuKey, checked)
  
  const menuNames = {
    devTest: '开发测试',
    storyDriven: '故事驱动', 
    miaomaTest: '妙码测试'
  }
  
  const menuName = menuNames[menuKey]
  message.success(`${menuName}菜单已${checked ? '启用' : '禁用'}`)
}

const handleResetDevSettings = async () => {
  saving.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 500))
    resetSettings()
    handleReset()
    message.success('开发设置已重置')
  } finally {
    saving.value = false
  }
}

const toggleAllMenus = () => {
  const newState = !allMenusEnabled.value
  console.log('Toggle all menus:', newState) // 调试日志
  
  Object.keys(menuToggles).forEach(key => {
    const menuKey = key as keyof typeof menuToggles
    toggleMenuItem(menuKey, newState)
  })
  
  message.info(newState ? '已开启所有开发菜单' : '已关闭所有开发菜单')
}

// 初始化时加载设置
const loadSettings = () => {
  try {
    const saved = localStorage.getItem('dev-tools-settings')
    if (saved) {
      Object.assign(debugSettings, JSON.parse(saved))
    }
  } catch (error) {
    console.warn('Failed to load dev tools settings:', error)
  }
}

loadSettings()
</script>

<style scoped>
/* 整体容器样式 */
.development-config-container {
  background: linear-gradient(135deg, #f8fafc 0%, #ffffff 100%);
  min-height: 100vh;
  border-radius: 12px;
  overflow: hidden;
}

/* 优化的页面头部 */
.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0;
  position: relative;
  overflow: hidden;
}

.page-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 20"><defs><radialGradient id="a" cx="50%" cy="50%" r="50%"><stop offset="0%" stop-color="rgba(255,255,255,.1)"/><stop offset="100%" stop-color="rgba(255,255,255,0)"/></radialGradient></defs><rect width="100" height="20" fill="url(%23a)"/></svg>');
  opacity: 0.1;
}

.header-content {
  position: relative;
  padding: 32px 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.header-icon {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  padding: 16px;
  font-size: 32px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.header-text h1 {
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 700;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-text p {
  margin: 0;
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  line-height: 1.5;
}

.header-right {
  display: flex;
  align-items: center;
}

.status-indicator {
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
}

.status-indicator.active {
  background: rgba(16, 185, 129, 0.2);
  border-color: rgba(16, 185, 129, 0.3);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ef4444;
  transition: all 0.3s ease;
}

.status-indicator.active .status-dot {
  background: #10b981;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.status-indicator span {
  font-size: 14px;
  font-weight: 500;
  color: white;
}

/* 内容区域 */
.config-content {
  padding: 40px;
}

.config-grid {
  display: grid;
  gap: 32px;
}

/* 主控制面板 */
.control-panel {
  background: white;
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid #f1f5f9;
  transition: all 0.3s ease;
}

.control-panel:hover {
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  transform: translateY(-2px);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 20px;
  font-weight: 600;
  color: #1e293b;
}

.panel-icon {
  font-size: 24px;
}

.main-switch {
  transform: scale(1.2);
}

.panel-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.control-description p {
  margin: 0 0 12px 0;
  color: #64748b;
  font-size: 15px;
  line-height: 1.6;
}

.feature-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.quick-actions {
  padding-top: 16px;
  border-top: 1px solid #f1f5f9;
}

.quick-action-btn {
  color: #3b82f6;
  font-weight: 500;
}

.quick-action-btn:hover {
  color: #1d4ed8;
  background: rgba(59, 130, 246, 0.1);
}

/* 菜单项配置区域 */
.menu-items-section {
  animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.section-header {
  margin-bottom: 24px;
}

.section-header h3 {
  margin: 0 0 8px 0;
  font-size: 22px;
  font-weight: 600;
  color: #1e293b;
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-icon {
  color: #6366f1;
}

.section-subtitle {
  color: #64748b;
  font-size: 15px;
  margin: 0;
}

.menu-items-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 20px;
}

/* 菜单项卡片 */
.menu-item-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  border: 2px solid #f1f5f9;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.menu-item-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: #e2e8f0;
  transition: all 0.3s ease;
}

.menu-item-card.active::before {
  background: linear-gradient(90deg, #10b981, #06d6a0);
}

.menu-item-card:hover {
  border-color: #e2e8f0;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.menu-item-card.active {
  border-color: #10b981;
  background: linear-gradient(135deg, #ecfdf5 0%, #ffffff 100%);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.menu-switch {
  z-index: 10;
  position: relative;
}

.menu-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: bold;
}

.dev-test-icon {
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
}

.story-driven-icon {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: white;
}

.miaoma-test-icon {
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
  color: white;
}

.card-content h4 {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
}

.menu-description {
  margin: 0 0 12px 0;
  color: #64748b;
  font-size: 14px;
  line-height: 1.5;
}

.menu-stats {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* 开发工具配置区域 */
.dev-tools-section {
  margin-top: 32px;
}

.tools-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 24px;
  margin-top: 20px;
}

.tool-category-card {
  background: white;
  border-radius: 16px;
  padding: 28px;
  border: 1px solid #f1f5f9;
  transition: all 0.3s ease;
}

.tool-category-card:hover {
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.category-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 2px solid #f8fafc;
}

.category-icon {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}

.debug-icon {
  background: linear-gradient(135deg, #06b6d4, #0891b2);
  color: white;
}

.api-icon {
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
  color: white;
}

.category-header h4 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
}

.category-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.tool-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: #f8fafc;
  border-radius: 10px;
  transition: all 0.2s ease;
}

.tool-item:hover {
  background: #f1f5f9;
}

.tool-info {
  flex: 1;
}

.tool-name {
  margin: 0 0 4px 0;
  font-weight: 500;
  color: #1e293b;
  font-size: 15px;
}

.tool-desc {
  margin: 0;
  color: #64748b;
  font-size: 13px;
  line-height: 1.4;
}

/* 底部操作区域 */
.action-section {
  margin-top: 40px;
  background: white;
  border-radius: 16px;
  padding: 32px;
  border: 1px solid #f1f5f9;
}

.action-header {
  margin-bottom: 24px;
  text-align: center;
}

.action-header h3 {
  margin: 0 0 8px 0;
  font-size: 20px;
  font-weight: 600;
  color: #1e293b;
}

.action-subtitle {
  color: #64748b;
  font-size: 15px;
  margin: 0;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: center;
}

.primary-actions, .danger-actions {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
}

.save-btn {
  background: linear-gradient(135deg, #10b981, #059669);
  border: none;
  height: 48px;
  padding: 0 32px;
  font-weight: 600;
  font-size: 16px;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.save-btn:hover {
  background: linear-gradient(135deg, #059669, #047857);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(16, 185, 129, 0.3);
}

.reset-btn {
  height: 48px;
  padding: 0 32px;
  font-weight: 500;
  font-size: 16px;
  border-radius: 12px;
  border: 2px solid #e2e8f0;
  transition: all 0.3s ease;
}

.reset-btn:hover {
  border-color: #cbd5e1;
  transform: translateY(-1px);
}

.danger-btn {
  height: 48px;
  padding: 0 32px;
  font-weight: 500;
  font-size: 16px;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.danger-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
}

.action-tips {
  margin-top: 24px;
  max-width: 600px;
  margin-left: auto;
  margin-right: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    gap: 20px;
    text-align: center;
  }
  
  .config-content {
    padding: 20px;
  }
  
  .menu-items-grid {
    grid-template-columns: 1fr;
  }
  
  .tools-grid {
    grid-template-columns: 1fr;
  }
  
  .primary-actions, .danger-actions {
    flex-direction: column;
    width: 100%;
  }
  
  .save-btn, .reset-btn, .danger-btn {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .header-content {
    padding: 24px 20px;
  }
  
  .header-text h1 {
    font-size: 24px;
  }
  
  .config-content {
    padding: 16px;
  }
  
  .control-panel, .tool-category-card, .action-section {
    padding: 20px;
  }
}
</style>